/*
 * @(#)StoreField.java
 *
 *
 *
 * ver 1.0 Sep 18, 2007 ggranum
 */
package org.testfire.realm.view.extjs.data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.testfire.realm.view.tags.util.JavaScriptUtilities;

public class StoreField
{

    public static final int TYPE_STRING = 1;

    public static final int TYPE_INT = 2;

    public static final int TYPE_FLOAT = 3;

    public static final int TYPE_BOOLEAN = 4;

    public static final int TYPE_DATE = 5;

    public static final int TYPE_OBJECT = 6;

    private static final String[] TYPE_STRINGS = {"string", "int","float","boolean","date","object"};

    String name;

    int type;

    public StoreField( String name, int type )
    {
        this.name = name;
        this.type = type;
    }

    public String getName()
    {
        return name;
    }

    public int getType()
    {
        return type;
    }

    public String getTypeString(){
        return TYPE_STRINGS[this.type];
    }


    public static String toJsonString(List<StoreField> fields){
        StringBuilder b = new StringBuilder();
        b.append("[ ");
        for(StoreField f : fields){
            b.append( f.toJsonString()).append( "," );
        }
        if(fields.size() > 0){
            b.replace( b.length() - 1, b.length(), " ");
        }
        b.append( " ]" );
        return b.toString();
    }

    public String toJsonString(){
        return "{ name: '" + name + "' , type: '" + getTypeString() + "'}";
    }



    public boolean isBooleanType()
    {
        return type == TYPE_BOOLEAN;
    }

    /**
     * Use this StoreField to convert an object to a value, such as a number or a float or a date, to a proper
     * JSON representation of said value corresponding to the datatype stored in this StoreField.
     * @param value A value to JSONify.
     * @return A proper string representation of the passed object, with respect to this StoreFields datatype.
     */
    public String getValueJson( Object value )
    {
        if ( type == TYPE_STRING )
        {
            value = "'" + value + "'";
        }
        else if ( type == TYPE_DATE )
        {
            if ( value instanceof Date )
            {
                value = "'" + JavaScriptUtilities.toJSDateTime( (Date)value ) + "'";
            }
            else
            {
                value = "'" + value + "'";
            }
        }

        return name + ":" + value;
    }

    /**
     * Create a list of JsonField objects from a JSON notation array, as per Ext.data.JsonStore.
     * @param fields A JSON String of fields, of the form [{name: 'fieldName', type: 'type }].
     * @return A list of JsonFields.
     * @throws JSONException On bad format of <code>fields</code>
     */
    public static List<StoreField> parse( String fields ) throws JSONException
    {

        List<StoreField> list = new ArrayList<StoreField>();
        JSONArray ary = new JSONArray( fields );
        for ( int i = 0; i < ary.length(); i++ )
        {
            Object x = ary.get( i );
            if ( x instanceof String )
            {
                list.add( new StoreField( (String)x, TYPE_STRING ) );
            }
            else
            {
                String name = (String)((JSONObject)x).get( "name" );
                String t = (String)((JSONObject)x).get( "type" );
                int type = TYPE_STRING;
                if ( t.equals( "float" ) )
                {
                    type = TYPE_FLOAT;
                }
                else if ( t.equals( "int" ) )
                {
                    type = TYPE_INT;
                }
                else if ( t.equals( "boolean" ) )
                {
                    type = TYPE_BOOLEAN;
                }
                else if ( t.equals( "date" ) )
                {
                    type = TYPE_DATE;
                }
                // default to string already set.

                list.add( new StoreField( name, type ) );
            }
        }
        return list;

    }
}

// eof: StoreField.java